package com.neutech.maplestory.client.entity;

import com.neutech.maplestory.client.client.MapleStoryClint;
import com.neutech.maplestory.client.constant.Constant;
import com.neutech.maplestory.client.util.ImageUtil;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * CLASS:Hero：
 * TODU:
 * date:2021/1/27、18:07
 * version:0.1
 */
public class Hero extends AbstractMapleStoryObject{
    public static Image[]imgs = new Image[100];
    static {
        for (int i = 0; i < 4; i++) {
            imgs[i] = ImageUtil.getImage("hero_right_stand"+i);
        }
        for (int i = 4; i < 8; i++) {
            imgs[i] = ImageUtil.getImage("hero_left_stand"+(i-4));
        }
        for (int i = 8; i < 13; i++) {
            imgs[i] = ImageUtil.getImage("hero_right_walk"+(i-8));
        }
        for (int i = 13; i < 18; i++) {
            imgs[i] = ImageUtil.getImage("hero_left_walk"+(i-13));
        }
        imgs[18] = ImageUtil.getImage("hero_right_prone");
        imgs[19] = ImageUtil.getImage("hero_left_prone");
        imgs[20] = ImageUtil.getImage("hero_right_jump");
        imgs[21] = ImageUtil.getImage("hero_left_jump");
        for (int i = 22; i < 26; i++) {
            imgs[i] = ImageUtil.getImage("hero_right_shoot"+(i-22));
        }
        for (int i = 26; i < 30; i++) {
            imgs[i] = ImageUtil.getImage("hero_left_shoot"+(i-26));
        }
    }
    public MapleStoryClint msc ;
    public Hero(){}
    public Hero(MapleStoryClint msc,int x,int y){
        this.x = x;
        this.y = y;
        this.speed = Constant.SPEED;
        this.width = imgs[0].getWidth(null);
        this.heigtht = imgs[0].getHeight(null);
        this.dir=Direction.RIGHT;
        this.action = Action.STAND;
        this.msc = msc;
    }

    /**
     * 一直加的计数器
     */
    public int count = 0;
    /**
     * 计算当前第几张图
     */
    private int step = 0;


    @Override
    public void draw(Graphics g) {
       /* if(count%5==0){
            g.drawImage(imgs[count++%4],x,y,null);
        }else {
            g.drawImage(imgs[count%4],x,y,null);
            count++;
        }*/
        switch (dir){
            case LEFT:
                switch (action){
                    case STAND:
                        if(count++ % 5 == 0){
                            g.drawImage(imgs[(step++ % 4)+4],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 4+4], x, y, null);
                        }
                        break;
                    case WALK:
                        if(count++ % 5 == 0){
                            g.drawImage(imgs[(step++ % 5)+13],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 5+13], x, y, null);
                        }
                        break;
                    case PRONE:
                        g.drawImage(imgs[19], x, y+30, null);
                        break;
                    case JUMP:
                        g.drawImage(imgs[21], x, y, null);
                        break;
                    case SHOOT:
                        if(count++ % 5 == 0){
                            g.drawImage(imgs[(step++ % 4)+26],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 4+26], x, y, null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action){
                    case STAND:
                        if(count++ % 5 == 0){
                            g.drawImage(imgs[step++ % 4],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 4], x, y, null);
                        }
                        break;
                    case WALK:
                        if(count++ % 5 == 0){
                            g.drawImage(imgs[(step++ % 5)+8],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 5+8], x, y, null);
                        }
                        break;
                    case PRONE:
                        g.drawImage(imgs[18], x, y+30, null);
                        break;
                    case JUMP:
                        g.drawImage(imgs[20], x, y, null);
                        break;
                    case SHOOT:
                        if(count++ % 5 == 0){
                            g.drawImage(imgs[(step++ % 4)+22],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 4+22], x, y, null);
                        }
                        break;
                    default:
                        break;
                }
                /*if(count++ % 5 == 0){
                    g.drawImage(imgs[step++ % 4],x,y,null);
                }else {
                    g.drawImage(imgs[step % 4], x, y, null);
                }*/
                break;
            default:
                break;
        }

        move();
        confirmStatus();
        outOfBound();
    }

    @Override
    public void move() {
        if(left){
            x-=speed;
            this.action = Action.WALK;
        }
        if (right){
            x+=speed;
            this.action = Action.WALK;
        }
        /*if(done){
            this.action = Action.PRONE;
        }*/
        if(jump){
            jump();
        }
        if(shoot){
            shoot();
        }
    }

    /**
     * 射击方法
     */
private int shootCount=0;
    private void shoot() {
        shootCount++;

        if(shootCount%10==0){
            shoot=false;
            Arrow arrow;
            if(dir==Direction.RIGHT){
                arrow = new Arrow(msc,x+width,y+heigtht/2+8,dir);
            }
            else {
                arrow = new Arrow(msc,x,y+heigtht/2+8,dir);
            }

            msc.arrows.add(arrow);
        }
    }

    private double vt = Constant.JUMP_SPEED;
    private double v0 ;
    private double g = 9.8;
    private double delta_height;
    private double t= 0.5;
    /**
     *自由落体
     */
    private void jump() {
        vt = v0 - g  *t;
        delta_height = v0*t;
        v0=vt;
        y-=delta_height;
        if(y>=410){
        jump = false;
        v0 = Constant.JUMP_SPEED;
        vt = 0.0;
        y= 410;
        }
    }

    /**
     * 根据当前人物的左右开头，确定人物的方向///////////////////////////BUG可以趴着跳！！！！！！！
     */
    private void confirmStatus(){
        if (left&&!right){
            this.dir = Direction.LEFT;
            if(jump){
                this.action = Action.JUMP;
            }
        }else if (right&&!left){
            this.dir = Direction.RIGHT;
            if(jump){
                this.action = Action.JUMP;
            }
        }else if(done){
            this.action = Action.PRONE;
        }else if(jump){
            this.action = Action.JUMP;
        }else if(shoot){
            this.action = Action.SHOOT;
        }
        //都按下，都不按下
        else {
            this.action = Action.STAND;
        }
    }
    /**
     *左右的开关
     *
     */
    public boolean left,right,done,jump,shoot;
    /**
     * 按键的方法
     * @param e 键盘事件对象
     */
    public void keyPressed(KeyEvent e){
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                left = true;
                break;
            case KeyEvent.VK_W:
                jump = true;
                break;
            case KeyEvent.VK_D:
                right = true;
                break;
            case KeyEvent.VK_S:
                done=true;
                break;
            case KeyEvent.VK_J://射击
                shoot=true;
                break;
            default:
                break;
        }
    }
    /**
     * 抬起的方法
     * @param e 键盘事件对象
     */
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                left = false;
                break;
            case KeyEvent.VK_W:
                //jump = false;
                break;
            case KeyEvent.VK_D:
                right = false;
                break;
            case KeyEvent.VK_S:
                done=false;
                break;
            case KeyEvent.VK_J://射击
               // shoot=false;
                break;
            default:
                break;
        }
    }

    /**
     *人物出界判断
     */
    private void outOfBound(){
        if(this.x<=0){
            this.x=0;
        }if(this.x>=Constant.GAME_WIDTH-this.width){
            this.x=Constant.GAME_WIDTH-this.width;
        }
    }
}
